package com.hmx.miaosha.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hmx.miaosha.domain.Goods;
import com.hmx.miaosha.domain.MiaoshaGoods;
import com.hmx.miaosha.mapper.GoodsMapper;
import com.hmx.miaosha.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: miaosha
 * @description:
 * @author: hmx
 * @create: 2021-10-08 13:54
 **/
@Service
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;



    public List<GoodsVo> listGoodsVo() {
        return goodsMapper.listGoodsVo();
    }


    public GoodsVo getGoodsVoByGoodsId(long goodsId) {
        return goodsMapper.getGoodsVoByGoodsId(goodsId);
    }


    @Transactional
    public boolean reduceStock(GoodsVo goods) {
        return goodsMapper.reduceStock(goods.getId()) > 0;
    }

    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    public List<Goods> list() {
        return goodsMapper.selectList(new LambdaQueryWrapper<Goods>());
    }

    public void update(Goods goods) {
        goodsMapper.updateById2(goods);
    }

    public void delete(Long id) {
        goodsMapper.deleteById(id);
    }

    public void resetStock(List<GoodsVo> goodsList) {
        for(GoodsVo goods : goodsList ) {
            MiaoshaGoods g = new MiaoshaGoods();
            g.setGoodsId(goods.getId());
            g.setStockCount(goods.getStockCount());
            goodsMapper.resetStock(g);
        }
    }
}
